using UnityEngine;
using System.Collections;

public class CubeEntity : Cube 
{

    private bool bIsSet = false;
    private Vector3 RotationAxis;
    private bool RotationDirection;

    // Update is called once per frame
    void Update() 
    {
        // test of how to set up coordinates
        // 24 vertices, 4 for every face, 6 faces
        if ( !bIsSet )
        {
            bIsSet = true;
            Vector2[] UVs = new Vector2[ 24 ];
            for ( uint i = 0; i < 24; ++i )
            {
                UVs[ i ].x = 0.5f;
                UVs[ i ].y = 0.5f;
            }
            SetUVCoordinate( UVs );
        }
        // test finished

        // test: use key to S and F to control the frame rotation
        bool IsKeyFDown = Input.GetKeyDown( KeyCode.F );
        bool IsKeySDown = Input.GetKeyDown( KeyCode.S );

        if ( IsKeyFDown )
        {
            // do a horizontal rotation based on one layer below the center
            StartCubeRotation( new Vector3( 1.0f, 0, 0 ), true );
            RotationSpeed = 200.0f;
        }
        else if ( IsKeySDown )
        {
            StartCubeRotation( new Vector3( 0, 1.0f, 0 ), true );
            RotationSpeed = 200.0f;
        }
        else
        {
            // not implemented
        }
        // test finished


        if ( IsRotationStarted )
        {
            CubeRotation( RotationAxis, RotationDirection );
        }
	}

    // interface: call to set the coordinates for the cube
    // Input:   
    //      UVs: 24 sets of 2D uv coordinates
    public bool SetUVCoordinate( Vector2[] UVs )
    {
        Mesh mesh = GetComponent<MeshFilter>().mesh;
        bool bResult = true;
        mesh.uv = UVs;

        return bResult;
    }

    // interface: call to start the cube rotation 90 degrees
    // Input:   
    //      vAxis: which axis to rotate
    //      bDirection: which direction Clockwise or Counter Clockwise
    public void StartCubeRotation( Vector3 vAxis, bool bDirection )
    {
        if ( !IsRotationStarted )
        {
            FrameCenter = transform.position;
            RotationAxis = vAxis;
            RotationDirection = bDirection;
            base.SetCubeRotationStart();
        }
    }

}
